package cn.bdqn.service.impl;

import cn.bdqn.dto.Dto;
import cn.bdqn.mapper.ItripHotelOrderMapper;
import cn.bdqn.mapper.ItripHotelTempStoreMapper;
import cn.bdqn.pojo.ItripHotelOrder;
import cn.bdqn.pojo.ItripHotelTempStore;
import cn.bdqn.service.ItripHotelOrderService;
import cn.bdqn.vo.StoreVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 2018/8/22
 */
@Service
public class ItripHotelOrderServiceImpl implements ItripHotelOrderService {
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;


    @Override
    public Dto insertOrderSelective(ItripHotelOrder record) {
        Dto d=new Dto();
        if (itripHotelOrderMapper.insertSelective(record)>0){
            d.setCode("0000");
            d.setMsg("增加成功");
        }else {
            d.setCode("10086");
            d.setMsg("增加失败");
        }
        return d;
    }

    @Override
    public ItripHotelOrder selectOrder(Long id) {
        return itripHotelOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public Dto updateByOrderSelective(ItripHotelOrder record) {
        Dto dto= new Dto();
        if (itripHotelOrderMapper.updateByPrimaryKeySelective(record)>0){
            dto.setCode("0000");
            dto.setMsg("修改成功");
        }else {
            dto.setCode("10086");
            dto.setMsg("修改失败");
        }
        return dto ;
    }

    @Override
    public Dto updateOrderWithBLOBs(ItripHotelOrder record) {
        Dto dto= new Dto();
        if (itripHotelOrderMapper.updateByPrimaryKeyWithBLOBs(record)>0){
            dto.setCode("0000");
            dto.setMsg("修改成功");
        }else {
            dto.setCode("10086");
            dto.setMsg("修改失败");
        }
        return dto ;
    }

    @Override
    public Dto updateOrder(ItripHotelOrder record) {
        Dto dto= new Dto();
        if (itripHotelOrderMapper.updateByPrimaryKey(record)>0){
            dto.setCode("0000");
            dto.setMsg("修改成功");
        }else {
            dto.setCode("10086");
            dto.setMsg("修改失败");
        }
        return dto ;
    }

    @Override
    public Dto updateStatus(Long id) {
        Dto dto = new Dto();
        ItripHotelTempStore itripHotelTempStore=itripHotelTempStoreMapper.selectByPrimaryKey(id);
        ItripHotelOrder itripHotelOrder = itripHotelOrderMapper.selectByPrimaryKey(id);

        //OrderStatus   0：待支付 1:已取消 2:支付成功 3:已消费 4：已点评 5：已删除
        //操作删除订单 将为0的变为1

        try {
            if (itripHotelOrder.getOrderStatus() != 5) {

                    int result=itripHotelOrderMapper.updateOrderStatus(id);
                    if (result>0){
                        dto.setMsg("修改成功");
                        dto.setCode("0000");

                    }else {
                        dto.setCode("10086");
                        dto.setMsg("修改失败");

                    }
                    return dto;
                }
        } catch (Exception e) {
            dto.setCode("10086");
            dto.setMsg("出现异常");
        }
        return dto;
        }
        @Override
    public boolean selectOrderCount(Map map){
        Integer count=(Integer)map.get("count");
        itripHotelTempStoreMapper.flushStore(map);

        List<StoreVO> list=itripHotelTempStoreMapper.queryRoomStore(map);
        for (StoreVO vo : list) {
            if (vo.getStore() < count) {
                return false;
            }
        }
        return true;
    }
}

